//
//  DYGroupSettingsViewController.m
//  DaYing
//
//  Created by DaYing on 2024/1/13.
//  Copyright © 2024 DaYing. All rights reserved.
//

#import "DYGroupSettingsViewController.h"

#import "DYGroupSettingsTextCell.h"
#import "DYGroupSettingsContentCell.h"
#import "DYGroupSettingsTitleCell.h"

#import "DYAddContentViewController.h"
#import "DYGroupingResultsViewController.h"

@interface DYGroupSettingsViewController ()

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *count;
@property (nonatomic,copy) NSString *content;
@property (nonatomic,copy) NSArray *groupList;

@end

@implementation DYGroupSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationItemTitleViewWithTitle:@"分组设置"];
    [self setRightItemWithTitle:@"保存" selector:@selector(saveButtonAction:)];

    [self.tableView registerNib:[UINib nibWithNibName:@"DYGroupSettingsTextCell" bundle:nil] forCellReuseIdentifier:@"DYGroupSettingsTextCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYGroupSettingsContentCell" bundle:nil] forCellReuseIdentifier:@"DYGroupSettingsContentCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYGroupSettingsTitleCell" bundle:nil] forCellReuseIdentifier:@"DYGroupSettingsTitleCell"];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight);
    [self.view bringSubviewToFront:self.navigationBar];
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
        
        subarr = [NSMutableArray arrayWithCapacity:1];

        dict =  @{@"class":DYGroupSettingsTextCell.class,
                  @"height":@([DYGroupSettingsTextCell getCellFrame:[NSNumber numberWithFloat:66]]),
                  @"data":[DYGroupSettingsTextCellModel ittemModelWithTitle:@"名称" content:_name tip:@"请输入名称"],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        dict =  @{@"class":DYGroupSettingsTextCell.class,
                  @"height":@([DYGroupSettingsTextCell getCellFrame:[NSNumber numberWithFloat:66]]),
                  @"data":[DYGroupSettingsTextCellModel ittemModelWithTitle:@"分组个数" content:_count tip:@"请输入分组个数"],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        dict =  @{@"class":DYGroupSettingsTitleCell.class,
                  @"height":@([DYGroupSettingsTitleCell getCellFrame:[NSNumber numberWithFloat:72]]),
                  @"data":[DYGroupSettingsTitleCellModel ittemModelWithTitle:@"分组内容"],
                  @"delegate":@YES};
        [subarr addObject:dict];
    
        [self.groupList enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger position = 0;
            if (self.groupList.count == 1) {
                position = 3;
            } else if (idx + 1 == self.groupList.count) {
                position = 2;
            } else if (idx > 0) {
                position = 1;
            }
            dict =  @{@"class":DYGroupSettingsContentCell.class,
                      @"height":@([DYGroupSettingsContentCell getCellFrame:[DYGroupSettingsContentCellModel ittemModelWithTitle:title position:position]]),
                      @"data":[DYGroupSettingsContentCellModel ittemModelWithTitle:title position:position]};
            [subarr addObject:dict];
        }];
                
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark DYGroupSettingsTextCellDelegate 名称 分组个数

- (void)groupSettingsTextCell:(DYGroupSettingsTextCell *)cell textField:(UITextField *)textField {
    if ([cell.model.title isEqualToString:@"名称"]) {
        _name = textField.text;
    } else if ([cell.model.title isEqualToString:@"分组个数"]) {
        _count = textField.text;
    }
}

#pragma mark DYGroupSettingsTitleCellDelegate 编辑

- (void)groupSettingsTitleCell:(DYGroupSettingsTitleCell *)cell button:(UIButton *)sender {
    [self gotoAddContentViewController];
}

#pragma mark - Action

//保存
- (void)saveButtonAction:(UIButton *)sender {
    __block NSString *erro = @"";
    
    if (_count.integerValue < 2) {
        erro = @"分组个数必须大于等于2";
    }
    
    if (_groupList.count < _count.integerValue) {
        erro = @"分组个数必须小于等于分组内容数量";
    }
    
    if (_groupList.count == 0) {
        erro = @"请添加分组内容";
    }
    
    if (_count.length == 0) {
        erro = @"请输入分组个数";
    }

    if (_name.length == 0) {
        erro = @"请输入名称";
    }

    if (erro.length > 0) {
        [self showProgressHUD:erro];
    } else {
        [self gotoGroupingResultsViewController];
    }
}

#pragma mark - goto

//添加内容
- (void)gotoAddContentViewController {
    DYAddContentViewController *vc = [DYAddContentViewController new];
    vc.content = _content;
    @weakify(self);
    vc.completion = ^(DYAddContentViewController *viewController, NSString *content) {
        @strongify(self);
        self->_content = content;
        NSArray *array = [content componentsSeparatedByString:@"\n"];
        self->_groupList = array;
        [self refreshData];
    };
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//分组结果
- (void)gotoGroupingResultsViewController {
    DYGroupingResultsViewController *vc = [DYGroupingResultsViewController new];
    vc.name = _name;
    vc.count = _count;
    vc.groupList = _groupList;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
